home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / a86v319c.zip / A10.DOC < prev    next >
Text File  |  1988-05-21  |  33KB  |  679 lines

  1. CHAPTER 10   RELOCATION AND LINKAGE
  2.  
  3.  
  4. A86 allows you to produce either .COM files, which can be run
  5. immediately as standalone programs, or .OBJ files, to be fed to
  6. the MS-DOS LINK program.  In this chapter I'll discuss .OBJ mode
  7. of A86.
  8.  
  9.  
  10. .OBJ Production Made Easy
  11.  
  12. I'll start by giving you the minimum amount of information you
  13. need to know to produce .OBJ files.  If you are writing short
  14. interface routines, and do not want to concern yourself with the
  15. esoterica of .OBJ files (segments, groups, publics, etc.), you
  16. can survive quite nicely by reading only this section.
  17.  
  18. There are two ways you can cause A86 to produce a .OBJ file as
  19. its object output.  One way is to explicitly give .OBJ as the
  20. output file name: for example, you can assemble the source file
  21. FOO.8 by giving the command "A86 FOO.8 FOO.OBJ".  The other way
  22. is to specify the switch +O (letter O not digit 0).  This is
  23. illustrated by the invocation "A86 +O FOO.8", which will have the
  24. same effect as the first invocation.
  25.  
  26. My design philosophy for .OBJ production is to accommodate two
  27. types of user.  The first type of user is writing new code, to
  28. link to other (usually high level language) modules.  That person
  29. should be able to write the module with a minimum of red tape,
  30. and have A86 do the right thing.  The second type of user has
  31. existing modules written for Intel/IBM assemblers, and wants to
  32. port them to A86.  A86 should recognize and act upon all the
  33. relocation directives (SEGMENT, GROUP, PUBLIC, EXTRN, NAME, END)
  34. given.  The assembly should work even if several files, assembled
  35. separately under the Intel/IBM assembler, are fed to a single A86
  36. assembly.  You'll see if you read on through this entire chapter
  37. that the multiple-files requirement causes A86 to interpret some
  38. of the relocation directives a little differently (while
  39. achieving compatible results).
  40.  
  41. Let's suppose you're writing new code: for example, an interface
  42. routine to the "C" language, that multiplies a 16-bit number by
  43. 10.  "C" pushes the input number onto the stack, before calling
  44. your routine.  Your code needs to get the number, multiply it by
  45. 10, and return the answer in the AX register.  You can code it:
  46.  
  47. _MUL10:          ; "C" expects all public names to start with "_"
  48.   PUSH BP        ; "C" expects BP to be preserved
  49.   MOV BP,SP      ; we use BP to address the stack
  50.   MOV AX,[BP+4]  ; fetch the number N, beyond BP and the ret addr
  51.   ADD AX,AX      ; 2N
  52.   MOV BX,AX      ; 2N is saved in BX
  53.   ADD AX,AX      ; 4N
  54.   ADD AX,AX      ; 8N
  55.   ADD AX,BX      ; 8N + 2N = 10N
  56.   POP BP         ; BP is restored
  57.   RET            ; go back to caller
  58.                                                              10-2
  59.  
  60. These 11 lines can be your entire source file!  If you name the
  61. file MUL10.8, A86 will create an object file MUL10.OBJ, that
  62. conforms to the standard SMALL model of computation for high
  63. level languages.  If you use RETF instead of RET (thus, by the
  64. way, getting the operand from BP+6 instead of BP+4), the object
  65. module will conform to the standard LARGE model of computation.
  66. All the red tape information required by the high level language
  67. is provided implicitly by A86.  I'll go through this information
  68. in detail later, but you should need to read about it only if
  69. you're curious.
  70.  
  71. What happens if you need to access symbols outside the module
  72. you're assembling?  If the type of the symbol is correctly
  73. guessed from the instruction that refers to it, then you can
  74. simply refer to it, and leave it undefined within the module. For
  75. example, if A86 sees the instruction CALL PRINT with PRINT
  76. undefined, it will assume that PRINT is a NEAR procedure.  If
  77. PRINT is never defined within the module, A86 will act as if you
  78. declared PRINT via the directive EXTRN PRINT:NEAR.  The address
  79. of PRINT will be plugged into your instruction by LINK when it
  80. combines A86's .OBJ file with the high level language's .OBJ
  81. files, to make the final program.
  82.  
  83. In general, the undefined operand to any CALL or JMP instruction
  84. is assumed to be NEAR.  The second (source) operand to a MOV or
  85. arithmetic instruction is assumed to be ABS (i.e., an immediate
  86. constant).  An undefined first (destination) operand is assumed
  87. to be a simple memory variable, of the same size (BYTE or WORD)
  88. as the register given in the second operand.  If your external
  89. symbol does not comply with these guidelines, you need to declare
  90. it with an EXTRN before you use it.  (You can also use EXTRN to
  91. declare types of non-complying forward references within your
  92. module, as you'll see later.)
  93.  
  94. If you'd like to link the MUL10 procedure to Turbo Pascal V4.0 or
  95. later, you need to append the line CODE SEGMENT PUBLIC to the top
  96. of the program, to name the program segment according to Turbo
  97. Pascal's expectations.  You may dispense with the leading
  98. underscore in the name MUL10-- Turbo Pascal does not require or
  99. expect it.
  100.  
  101. At this point, if you're a casual user, I think you've read
  102. enough to get going!  Read further only if you wish; or if you
  103. get stuck, and need to master the esoterica.
  104.                                                              10-3
  105.  
  106. Overview of Relocation and Linkage
  107.  
  108. When you assemble a program directly into a .COM file, the
  109. program has just two forms: the source program, that you can
  110. understand, and the .COM file, that the computer can "understand"
  111. (i.e., execute).  A .OBJ file is an intermediate format: neither
  112. you nor the (executing) computer can make sense out of a .OBJ
  113. file; only programs like LINK interpret .OBJ files.  The purpose
  114. of a .OBJ file is to allow you to assemble or compile just a part
  115. of a program.  The other parts (also in the form of .OBJ files)
  116. can be produced at a different time; often by a different
  117. assembler or compiler, whose source files are in a different
  118. language.  It's easy to see where the word "linkage" comes from:
  119. the LINK program puts the pieces of a program together.  The
  120. "relocation" comes because the assembler or compiler that makes a
  121. given program piece doesn't know how many other pieces will come
  122. before it, or how big the other pieces will be.  Each piece is
  123. constructed as if it started at location 0 within the program;
  124. then LINK "relocates" the piece to its true location.
  125.  
  126. Many of the relocation features of 86 assembly language are
  127. couched in terms of LINK's point of view, so we must look at the
  128. way LINK sees things.  LINK calls a .OBJ file an "object module",
  129. or just "module".  Each module has a NAME, that can be referred
  130. to when LINK issues diagnostic messages, such as error messages
  131. and symbol maps.  If a program symbol is used only within a
  132. single module, it does not need to be given to LINK, except
  133. possibly to pass along to a symbolic debugger.  On the other
  134. hand, if a program symbol is defined in one module and referenced
  135. in other modules, then LINK needs to know the name of the symbol,
  136. so it can resolve the references.  Such a symbol is PUBLIC in the
  137. module in which it is defined; it is "external" in the other
  138. modules, containing references to it.  Finally, exactly one
  139. module in a program must contain the starting location for the
  140. program; that module is called the "main module", and it must
  141. supply the starting address (which is not necessarily at the
  142. beginning of the module).
  143.  
  144. In the 86 family of microprocessors, the LINK system also does
  145. much to manage the memory segments that a program will fit into,
  146. and get its data from.  The (grotesquely ornate) level of support
  147. for segmentation was dictated by Intel when it specified (and IBM
  148. and the compiler makers accepted) the format that .OBJ files will
  149. have.  I attended the fateful meeting at Intel, in which the
  150. crucial design decisions were made.  I regret to say that I sat
  151. quietly, while engineers more senior than I applied their fertile
  152. imaginations to construct fanciful scenarios which they felt had
  153. to be supported by LINK.  Let's now review the resulting
  154. segmentation model.
  155.                                                              10-4
  156.  
  157. The parts of a program, as viewed by LINK, come in three
  158. different sizes: they can be (1) pieces of a single segment, (2)
  159. an entire single segment, or (3) a sequence of consecutive
  160. segments in 86 memory.  Size (1) should have been called
  161. something like FRAGMENT, but is instead called SEGMENT.  Size (2)
  162. should have been called SEGMENT, but is instead called GROUP.
  163. Size (3) should have been called "group", but is instead called
  164. "class".  Let me cling to the sensible terminology for one more
  165. paragraph, while I describe the worst scenario Intel wanted to
  166. support; then when I discuss individual directives, I'll
  167. regretfully revert to the official terminology.
  168.  
  169. The scenario is as follows: suppose you have a program that
  170. occupies about 100K bytes of memory.  The program contains a core
  171. of 20K bytes of utility routines that every part of the program
  172. calls.  You'd like every part of the program to be able to call
  173. these routines, using the NEAR form to save memory.  By gum, you
  174. can do it!  You simply(!) slice the program into three fragments:
  175. the utility routines will go into fragment U, and the rest of the
  176. program will be split into equal-sized 40K-byte fragments A and
  177. B.  Now you arrange the fragments in 8086 memory in the order
  178. A,U,B.  The fragments A and U form a 60K-byte block, addressed by
  179. a segment register value G1, that points to the beginning of A.
  180. The fragments U and B form another 60K-byte block addressed by a
  181. segment register value G2, that points to the beginning of U.  If
  182. you set the CS register to G1 when A is executing, and G2 when B
  183. is executing, the U fragment is accessible at all times.  Since
  184. all direct JMPs and CALLs are encoded as relative offsets, the U-
  185. code will execute direct jumps correctly whether addressed by G1
  186. with a huge offset, or G2 with a small offset.  Of course, if U
  187. contains any absolute pointers referring to itself (such as an
  188. indirect near JMP or CALL), you're in trouble.
  189.  
  190. It's now been over a decade since the fateful design meeting took
  191. place, and I can report that the above scenario has never taken
  192. place in the real world.  And I can state with some authority
  193. that it never will.  The reason is that the only programs that
  194. exceed 64K bytes in size are coded in high level language, not
  195. assembly language.  High level language compilers follow a very,
  196. very restricted segmentation model-- no existing model comes
  197. remotely close to supporting the scheme suggested by the
  198. scenario.  But the 86 assembly language can support it-- the
  199. directives "G1 GROUP A,U" and "G2 GROUP B,U", followed by chunks
  200. of code of the appropriate object size, headed by directives "A
  201. SEGMENT", "B SEGMENT", and "U SEGMENT".  The LINK program is
  202. supposed to sort things out according to the scenario; but I
  203. can't say (and I have my doubts) if it actually succeeds in doing
  204. so.
  205.  
  206. The concept of "class" was added as an afterthought, to implement
  207. the more sensible and usable features that outsiders thought
  208. GROUPs were implementing;  namely, the ability to specify that
  209. different (and disjoint!) segments occur consecutively in memory.
  210. This allows programs to be arranged in a consistent manner-- for
  211. example, with all program code followed by all static data
  212. segments followed by all dynamically allocated memory.
  213.                                                              10-5
  214.  
  215. The NAME Directive
  216.  
  217. Syntax:  NAME module_name
  218.  
  219. The NAME directive specifies that "module_name" be given to LINK
  220. as the name of the module produced by this assembly.  The symbol
  221. "module_name" can be used elsewhere in your program without
  222. conflict: it can even, if you like, be a built-in assembler
  223. mnemonic (e.g. "NAME MOV" is acceptable)!
  224.  
  225. If you do not provide a NAME directive, A86 will use the name of
  226. the output object file, without the .OBJ extension.   If you
  227. provide more than one NAME directive, A86 will use the last one
  228. given, with no error reported.
  229.  
  230.  
  231.  
  232. The PUBLIC Directive
  233.  
  234. Syntax:  PUBLIC  sym1, sym2, sym3, ...
  235.          PUBLIC
  236.  
  237. The PUBLIC directive allows you to explicitly list the symbols
  238. defined in this assembly, that can be used by other modules.  If
  239. you do not give any PUBLIC directives in your program, A86 will
  240. use every relocatable label and variable name in your program,
  241. except local labels (the redefinable labels consisting of a
  242. letter followed by digits: L7, M1, Q234, etc.).  Symbols EQUated
  243. to constants, and symbols defined within structures and DATA
  244. SEGMENTs, are not implicitly declared PUBLIC: you have to
  245. explicitly include them in a PUBLIC directive.
  246.  
  247. A86 maintains an internal flag, telling it whether to figure out
  248. for itself which symbols are PUBLIC, or to let the program
  249. explicitly declare them.  The flag starts out "implicit", and is
  250. set to "explicit" only if A86 sees a PUBLIC directive with no
  251. names at all, or a PUBLIC directive containing at least one name
  252. that would have been implicitly made PUBLIC.
  253.  
  254. If you are writing new code, you'll probably want to keep the
  255. flag "implicit".  You use the PUBLIC directive only for those
  256. symbols which have the form of local labels, but aren't (e.g., a
  257. memory variable I1987 for 1987 income); and for absolute values
  258. that are globally accessed -- e.g. specify "PUBLIC
  259. OPEN_FILES_LIMIT" for a symbol defined as "OPEN_FILES_LIMIT EQU
  260. 20".
  261.  
  262. If you are porting existing code, that code will already have
  263. PUBLIC directives in it, and A86 will go to "explicit" mode,
  264. duplicating the functionality of other assemblers.
  265.  
  266. The PUBLIC directive with no names is used to force "explicit"
  267. mode, thus causing (if there are no further PUBLICs with names)
  268. the .OBJ file to declare no symbols PUBLIC.
  269.                                                              10-6
  270.  
  271. There is another side effect to the PUBLIC directive: if a symbol
  272. is declared PUBLIC in a module, it had better be defined in that
  273. module.  If it isn't then A86 includes it in the .ERR listing of
  274. undefined symbols in the module, and suppresses output of the
  275. object file.
  276.  
  277.  
  278. The EXTRN Directive
  279.  
  280. Syntax:  EXTRN  sym1:type, sym2:type, ...
  281.  
  282. where "type" is one of:  BYTE WORD DWORD QWORD TBYTE FAR
  283. or synonymously:           B    W    D     Q     T     F
  284. or:                        NEAR  ABS
  285.  
  286. The EXTRN directive allows you to attach a type to a symbol that
  287. may not yet be defined (and may never be defined) within your
  288. program.  This is often necessary for the assembler to generate
  289. the correct instruction form when the symbol is used as an
  290. operand.  All the possible types except ABS are defined elsewhere
  291. in the A86 language, but I list them again here for convenience:
  292.  
  293.    B or BYTE:   byte-sized memory variable
  294.    W or WORD:   word (2 byte) sized memory variable
  295.    D or DWORD:  doubleword (4-byte) sized memory variable
  296.    Q or QWORD:  quadword (8-byte) sized memory variable
  297.    T or TWORD:  10-byte-sized memory variable
  298.    NEAR:        program label accessed within a segment
  299.    FAR:         program label accessed from outside this segment
  300.    ABS:         an absolute number (i.e., an immediate constant)
  301.  
  302. An example of EXTRN usage is as follows: suppose there is a word
  303. memory variable IFARK in your program.  The variable might be
  304. declared at the end of the program; or it might be defined in a
  305. module completely outside of this program.  Without an EXTRN
  306. directive, A86 will assemble an instruction such as "MOV
  307. AX,IFARK" as the loading of an immediate constant IFARK into the
  308. AX register.  If you place the directive "EXTRN IFARK:W" at the
  309. top of your program, you'll get the correct instruction form for
  310. MOV AX,IFARK-- moving a word memory variable into the AX
  311. register.
  312.  
  313. A86 will allow more than one EXTRN directive for a given symbol,
  314. as long as the same type is given every time.  A86 will even
  315. allow an EXTRN directive for a symbol that has already been
  316. defined, as long as the type declared is consistent with the
  317. symbol's definition.  These allowances exist so that you can
  318. assemble multiple files written for another assembler, that had
  319. been fed separately to that assembler.
  320.                                                              10-7
  321.  
  322. Note that EXTRN is viewed quite differently by A86 than by other
  323. assemblers.  In fact, if it weren't for those other assemblers,
  324. I'd use the mnemonic DECLARE instead of EXTRN.  A86 doesn't
  325. really use EXTRN to determine which symbols are external-- it
  326. uses those symbols that are undefined at the end of assembly.  As
  327. I stated earlier in the chapter, an undefined symbol can be
  328. referenced without being declared via EXTRN. Conversely, a
  329. defined symbol can be declared (and redeclared) via EXTRN; being
  330. defined, such a symbol will not be specified "external" in the
  331. .OBJ file.
  332.  
  333. Because EXTRN is useful in forward reference situations, it is
  334. now recognized even when A86 is assembling a .COM file.
  335.  
  336. For those of you who are accustomed to the more traditional use
  337. of EXTRN, and who do not like external records to be created
  338. "behind your back", A86 offers the "+x" option.  If you include
  339. "+x" in the program invocation, A86 will require that all
  340. undefined symbols be explicitly declared via an EXTRN.  Any
  341. undefined, undeclared symbols will be included in the .ERR
  342. listing of undefined symbols, and object file output will be
  343. suppressed.
  344.  
  345.  
  346. MAIN: The Starting Location for a Program
  347.  
  348. I've already stated that exactly one module in a program is the
  349. "main" module, containing the starting address of the entire
  350. program.  In A86 when assembling .OBJ files, the starting address
  351. is given by the label MAIN.  You simply provide the label "MAIN:"
  352. where you want the program to start.  The module containing MAIN
  353. is the main module.
  354.  
  355.  
  356. The END Directive
  357.  
  358. Syntax:  END
  359.          END start_addr
  360.  
  361. The END directive is used by other assemblers for two purposes,
  362. both of which are now a little silly.  The first purpose is to
  363. signal the end of assembly.  This was necessary back in the days
  364. when source files were input on media such as paper tape: you had
  365. to tell the assembler explicitly that the content of the tape has
  366. ended.  Today the operating system can tell you when you've
  367. reached the end of the file, so this function is an anachronism.
  368.  
  369. The second purpose of END is, nonsensically, to allow you to
  370. specify the starting location of the program.  I suppose the
  371. person who wrote the first assembler back in the 1950's was too
  372. short on memory to implement a separate START directive, or a
  373. MAIN label like A86 has, and decided to let END do double duty.
  374. I've always considered the example "END START" to have an Alice-
  375. in-Wonderland quality; it is fuel for the high-level-language
  376. snobs who like to attack assembly language.  Please defeat the
  377. snobs, and use "MAIN:" if you are writing new code.
  378.                                                              10-8
  379.  
  380. For compatibility, A86 treats "END start_addr" exactly the same
  381. as if you had coded "MAIN EQU start_addr".  Note that if you want
  382. your program to assemble under both A86 and that other assembler,
  383. you can specify "END MAIN"-- A86 treats MAIN EQU MAIN as a legal
  384. redefinition of the symbol MAIN.
  385.  
  386. A86 ignores END when there is no starting-address operand, thus
  387. allowing assembly of multiple files written for other assemblers.
  388.  
  389.  
  390. The SEGMENT Directive
  391.  
  392. Syntax:  seg_name SEGMENT [align] [combine] ['class_name']
  393.  
  394. where "align" is one of:  BYTE   WORD   PARA   PAGE
  395. "combine" is one of:      PUBLIC  STACK  COMMON  MEMORY
  396.                             AT number
  397.  
  398. The SEGMENT directive says that assembled object code will
  399. henceforth go to a block of code whose name is "seg_name".
  400. "seg_name" is a symbol that represents a value that can be loaded
  401. into a segment register.  If "seg_name" is not declared in a
  402. GROUP directive, then its value should in fact be loaded into a
  403. segment register, in order to address the code.  If "seg_name" is
  404. declared in a GROUP directive, then the code is a a part of the
  405. segment addressed by the name of the group.
  406.  
  407. A program can consist of any number of named segments, to be
  408. combined in numerous exotic ways to produce the final program.
  409. You can redirect your object output from one segment to another
  410. in your assembly, by providing a SEGMENT directive before each
  411. piece of code.  You can even return to a segment you started
  412. earlier, by repeating a SEGMENT with the same name-- the
  413. assembler just picks up where it left off, subject to some
  414. possible skipping for memory alignment, that I'll describe
  415. shortly.
  416.  
  417. The specifications following the word SEGMENT help to describe
  418. how the code in this module's part of the segment will be
  419. combined with code for the same segment name given in other
  420. modules; and also how this named segment will be grouped with
  421. other named segments.  Other assemblers require the
  422. specifications to be given in the order indicated.  A86 will
  423. accept any order, and will accept commas between the
  424. specifications if you want to provide them.  The only restriction
  425. is that "AT number" must be followed by a comma if it is not the
  426. last specification on the line.
  427.                                                              10-9
  428.  
  429. The "align" specification tells if each piece of code within the
  430. segment should be aligned so that its starting address is an even
  431. multiple of some number.  BYTE alignment means there is no
  432. requirement; WORD alignment requires each piece to start at a
  433. multiple of 2; PARA alignment, at a multiple of 16; PAGE
  434. alignment, at a multiple of 256.  For example, suppose you have a
  435. segment containing memory variables.  You can declare the segment
  436. with the statement "VAR_DATA SEGMENT WORD", which insures that
  437. the segment is aligned to an even memory address.  That way you
  438. can insure that all 16-bit and bigger memory quantities in the
  439. segment are aligned to even addresses, for faster access on the
  440. 16-bit machines of the 86 family.
  441.  
  442. There are special rules governing alignment for multiple pieces
  443. of the same named segment within the same program module.  Other
  444. assemblers outlaw conflicting alignment specifications in this
  445. situation; A86 accepts them, and uses the strictest specification
  446. given.  Furthermore, the alignment given for any specification
  447. beyond the first will control the alignment for that piece of
  448. code within this module's chunk.  For example, if a program
  449. contains two pieces of code headed by "VAR_DATA SEGMENT WORD",
  450. A86 will insert a byte between the pieces if the first piece has
  451. an odd number of bytes.  This insures correct assembly for
  452. multiple files written for another assembler.
  453.  
  454. If no "align" type is given for any of the pieces of a named
  455. segment, an alignment of PARA is assumed.
  456.  
  457.  
  458. The "combine" specification tells how the chunk of code from this
  459. module will be combined with the chunks of the same named
  460. segment, that come from other modules.  Yes, I know, that sounds
  461. like what "align" does; but "combine" takes a different, more
  462. major point of view:
  463.  
  464. * PUBLIC is the kind of combination we've been talking about all
  465.   along: each piece of the segment is located off the end of the
  466.   previously linked piece, subject to possible gaps for
  467.   alignment.  The size of the segment is the sum of the sizes of
  468.   the pieces, plus the sizes of the gaps.
  469.  
  470. * STACK is a combination type reserved for the system's stack
  471.   segment.  To illustrate how STACK segment chunks are combined,
  472.   let's describe the only way a stack segment should ever be
  473.   used.  We'll call the segment STACK; and we declare it as
  474.   follows:
  475.  
  476.     STACK SEGMENT WORD STACK
  477.         DW 100 DUP (?)
  478.     TOP_OF_STACK:
  479.                                                             10-10
  480.  
  481.   The code just given declares a stack area of 200 bytes (100
  482.   words) for this module.  If identical code occurs in each of
  483.   three modules which are then linked together, the resulting
  484.   STACK segment will have 600 bytes (the sizes are added), but
  485.   TOP_OF_STACK will be the same address (600) for each module
  486.   (each piece is overlayed at the top of the segment).  That way,
  487.   every module can declare and access the top of the stack, which
  488.   is the only static part of the stack that any code should ever
  489.   refer to.
  490.  
  491. * COMMON is a type of memory area supported by FORTRAN.  Each
  492.   module's chunk of a COMMON segment starts at location 0, and
  493.   overlaps (usually duplicates) the pieces from all the other
  494.   modules.  The size of a COMMON segment is the size of the
  495.   largest chunk.
  496.  
  497. * MEMORY is supposed to be another kind of COMMON segment, that
  498.   is distinguished by automatically being located beyond all
  499.   other segments in memory.  The MS_DOS LINK program, however,
  500.   does not implement MEMORY segments, and instead treats them
  501.   identically to PUBLIC (not COMMON!) segments.  I can see no
  502.   useful purpose to the MEMORY combine type, since the
  503.   functionality can be achieved by putting a COMMON segment into
  504.   a 'class' by itself, that goes above all the other classes.  So
  505.   don't use MEMORY.
  506.  
  507.   Sorry, I don't support the assembly of multiple files written
  508.   for other assemblers, that contain STACK, COMMON, or MEMORY
  509.   segments.  If I did, I would have to detect the file breaks,
  510.   and duplicate the overlapping functionality of these segment
  511.   types.  Since I don't think anybody out there is using these
  512.   esoteric types, I didn't bother to support them to that extent.
  513.   Objections, anyone?
  514.  
  515. * "AT number" defines a non-combinable segment at the absolute
  516.   memory location whose segment register value is "number".  This
  517.   form is useful for initializing data in fixed locations, such
  518.   as the 86 interrupt vector (IVECTOR SEGMENT AT 0 followed by
  519.   ORG 4 * INT_NUMBER), or for reading fixed memory locations,
  520.   such as the BIOS variables area (BIOS_DATA SEGMENT AT 040).
  521.  
  522. The combine type specification can be repeated in subsequent
  523. pieces of a given segment, but if it is, it must be the same in
  524. all pieces.
  525.  
  526. Finally, if no combine type is ever given for a named segment in
  527. a module, that segment is non-combinable-- no other modules may
  528. define that segment; the code given in the one module constitutes
  529. the entire segment.
  530.                                                             10-11
  531.  
  532. The last specification available on a SEGMENT line is the class
  533. name, which is identified by being enclosed in single quotes.
  534. Unlike a segment name, which can be used as an instruction
  535. operand and hence cannot conflict with other assembler symbols, a
  536. class name can be assigned without regard to its usage elsewhere
  537. in the program.  It can even be a built-in A86 mnemonic.  In
  538. fact, both the SMALL and LARGE high-level-language models specify
  539. the class name 'CODE' for code segments, and the SMALL model
  540. specifies the class name 'DATA'.
  541.  
  542. If no class name is given for a segment, A86 specifies the null
  543. (zero length) string as the class name.
  544.  
  545.  
  546. DATA SEGMENT, STRUC and CODE SEGMENT Directives
  547.  
  548. The DATA SEGMENT and STRUC directives work in .OBJ mode exactly
  549. as they do in .COM mode-- they define a special assembly mode, in
  550. which declarations are made, but no object code is output.
  551. Offsets within DATA segments and structures are absolute, as in
  552. .COM mode.  Assembly resumes as before when an ENDS or CODE
  553. SEGMENT directive is encountered.
  554.  
  555. For MASM compatibility (especially in modules written to link to
  556. Turbo Pascal V4.0 programs), I now recognize the keywords CODE,
  557. DATA, and STACK as ordinary relocatable segment names.  The
  558. ordinary functionality takes effect whenever a SEGMENT directive
  559. is given with CODE, DATA or STACK as the segment name, and with
  560. one or more relocatable parameters (e.g., PUBLIC) given after
  561. SEGMENT.
  562.  
  563.  
  564. The ENDS Directive
  565.  
  566. Syntax:  [seg_name] ENDS
  567.  
  568. The ENDS directive closes out the segment currently being
  569. assembled, and returns assembly to the segment being assembled
  570. before the last SEGMENT directive.  The "seg_name", if given,
  571. must match the name in that last SEGMENT directive.  ENDS allows
  572. you to "nest" segments inside one another.  For example, you can
  573. declare some static data variables that are specific to a certain
  574. section of code at the top of that section:
  575.  
  576.   _DATA SEGMENT BYTE PUBLIC 'DATA'
  577.       VAR1  DB ?
  578.       VAR2  DB ?
  579.   _DATA ENDS
  580.  
  581. These four lines can be inserted inside any other segment being
  582. assembled.  They will cause the two variable allocations to be
  583. tacked onto the segment _DATA; and assembly will then continue in
  584. whatever segment surrounded the four lines.  Observe that the
  585. "nesting" does not occur in the final program; only the
  586. presentation of the source code is nested.
  587.                                                             10-12
  588.  
  589. If you are not nesting segments inside one another, then the ENDS
  590. directive serves only to lend a clean, "block-structured"
  591. appearance to your source code.  It does not assist A86 in any
  592. particular way; in fact, it consumes a bit more object output
  593. memory (slightly reducing object output capacity) if you have
  594. ENDSs, rather than just starting up new segments with SEGMENT
  595. directives.
  596.  
  597.  
  598. Default Outer SEGMENT
  599.  
  600. Other assemblers outlaw any code outside of a SEGMENT
  601. declaration, forcing you to give a SEGMENT declaration before you
  602. can assemble anything.  A86 lets you assemble just your code; you
  603. don't have to worry about SEGMENTs if you don't want to.
  604.  
  605. If you do provide code outside of all SEGMENT declarations, A86
  606. performs the following steps, to find a reasonable place to put
  607. the code:
  608.  
  609. 1. If there are any segments explicitly declared whose name is or
  610.    ends with "_TEXT", then the first such segment declared is
  611.    used.  It is as if the SEGMENT declaration appeared at the top
  612.    of, rather than within, the program.
  613.  
  614. 2. If there is no such explicit segment, A86 creates a BYTE
  615.    PUBLIC segment of class 'CODE', and proceeds to construct a
  616.    name for the segment.  If there are no RETF instructions in
  617.    the outer segment, the name chosen is "_TEXT", conforming to
  618.    the SMALL model of computation.  If there is a RETF
  619.    instruction, the name chosen is "modulename_TEXT", where
  620.    "modulename" is the name of this module.  Recall that
  621.    "modulename" comes from the NAME directive if there is one;
  622.    from the name of the .OBJ file if there isn't.
  623.  
  624.  
  625. The GROUP Directive
  626.  
  627. Syntax:  group_name GROUP seg_name1, seg_name2, ...
  628.  
  629. The GROUP directive causes A86 to tell LINK that all the listed
  630. segments can fit into a single 64K-byte block of memory, and
  631. instruct LINK to make that fit.  (If they won't fit, LINK will
  632. issue an error message.)  Having declared the group, you can then
  633. use "group_name" as the segment register value that will allow
  634. simultaneous access to all the named segments.  The order of
  635. names given in the list does not necessarily determine the order
  636. the segments will finally appear within the group.
  637.  
  638. The most useful application of the GROUP directive is to allow
  639. you to structure the pieces of a program, all of whose code and
  640. data will fit into a single 64K segment.  You organize the pieces
  641. into SEGMENTs, and declare all the SEGMENTs to be within the same
  642. GROUP.  When the program starts, all segment registers are set to
  643. point to the GROUP, and you never have to worry about segment
  644. registers again in the program.
  645.                                                             10-13
  646.  
  647. WARNING: If your segments will be GROUPed in the final program,
  648. you should have the appropriate GROUP directive in every module
  649. assembled.  If you don't, then any memory pointers generated will
  650. be relative to the beginning of the individual named segments,
  651. not to the beginning of the whole group.
  652.  
  653. Because of the obscure scenario I described in the Overview
  654. section, Intel does not prohibit more than one GROUP from
  655. containing some of the same segments; so neither does A86.  Any
  656. pointers within a segment will be calculated from the beginning
  657. of the last GROUP that the segment was declared within.  But
  658. again, I have my doubts as to whether LINK will handle this
  659. correctly.
  660.  
  661.  
  662. The SEG Operator
  663.  
  664. Syntax:  SEG operand
  665.  
  666. The SEG operator is provided for compatibility with other
  667. assemblers, and with some compilers such as Turbo C.  The operand
  668. to SEG should be a variable or label that is either undefined, or
  669. declared within a relocatable segment.  The result of the SEG
  670. operator is the segment containing the variable or label.  If the
  671. segment cannot be determined by A86, it will be plugged in by
  672. LINK.
  673.  
  674. SEG is legal only when A86 is assembling to an OBJ file.  Since
  675. segment locations are determined at load time, and since COM
  676. files have no facility for specifying the location of segment
  677. references, such references are always disallowed in COM files.
  678.  
  679.